<?php
namespace app\service;

use app\model\CamperModel;
use const support\CORRECT;
use const support\DENY;

class CamperService
{

    //获取列表
    public static function lists(int $memberId): array
    {
        return CamperModel::where('status',CORRECT)
            ->where('member_id',$memberId)
            ->orderBy('create_time','desc')->select([
                'id','real_name','card_number','member_type'
            ])->get()->toArray()?:[];
    }

    //添加营员
    public static function toAdd($memberId,$insert): int
    {
        $insert['member_id']=$memberId;
        return CamperModel::insertGetId($insert);
    }

    //更新营员
    public static function toEdit($memberId,$id,$update){
        $update['update_time']=time();
        return CamperModel::where([
            'id'        =>$id,
            'member_id' =>$memberId,
            'status'    =>CORRECT
        ])->update($update);
    }

    //删除营员
    public static function toDel($memberId,$id){
        return CamperModel::where([
            'id'        =>$id,
            'member_id' =>$memberId
        ])->update([
            'update_time'=>time(),
            'status'     =>DENY
        ]);
    }

    //营员详情
    public static function details(int $memberId,int $id): array
    {
        $info= CamperModel::where([
            'id'        =>$id,
            'member_id' =>$memberId
        ])->select([
            'id',
            'real_name',
            'card_number',
            'member_icon',
            'member_height',
            'member_weight',
            'member_type',
            'guardian_name',
            'guardian_mobile',
            'urgent_contact_name',
            'urgent_contact_mobile',
            'remarks',
            'status'
        ])->first();
        return !empty($info->id)?$info->toArray():[];
    }

}